﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.Mvc;

namespace CoolCode.Web.Mvc.UI.Grid
{
	public class PagerRenderer : IPagerRenderer
	{
		public void Render(IPagerModel pagerModel, IPageable dataSource, TextWriter output, ViewContext viewContext)
		{
			output.Write("<div class=\"pDiv\">");
			output.Write("<div class=\"pDiv2\">");
			Pager pager = new GridPager(dataSource, viewContext);
			if (!string.IsNullOrEmpty(pagerModel.UpdateTarget))
			{
				//分页事件触发的脚本 
				LinkRender linkRender = new LinkRender();
				string script = linkRender.Render(pagerModel.UpdateTarget);
				pager = pager.OnClick(script);
			}
			pagerModel.PagerAction(pager);
			output.Write(pager);
			output.Write("</div>");
			output.Write("</div>");
		}

	}

	public class GridPager : Pager
	{
		public GridPager(IPageable dataSource, ViewContext viewContext)
			: base(dataSource, viewContext.HttpContext, viewContext.RequestContext, viewContext.RouteData.Values)
		{
			_summaryFormat = string.Format("<div class=\"pGroup\"><span class=\"pPageStat\">{0}</span></div>", _summaryFormat);
		}

		public override void Render(TextWriter output) 
		{
			output.Write("<div class=\"{0}\" align=\"{1}\">", _css, _position);
			if (this.TotalRecords > 0)
			{
				output.Write("<div class=\"pGroup\">");
				RenderPageLink(output, 0, _firstPageText, !HasPreviousPage, "pFirst pButton");
				RenderPageLink(output, PageIndex - 1, _previousPageText, !HasPreviousPage, "pPrev pButton");
				output.Write("</div>");

				RenderSeparator(output);
				output.Write("<div class=\"pGroup\">");
				output.Write("<span class=\"pcontrol\">");

				//Render numeric page
				if (_numericPageVisible)
				{
					RenderNumericPages(output);
				}

				output.Write("</span>");
				output.Write("</div>");
				RenderSeparator(output);

				output.Write("<div class=\"pGroup\">");
				RenderPageLink(output, PageIndex + 1, _nextPageText, !HasNextPage, "pNext pButton");
				RenderPageLink(output, PageCount - 1, _lastPageText, !HasNextPage, "pLast pButton");
				output.Write("</div>");
				RenderSeparator(output);

			}

			output.Write("<div class=\"pGroup\">");
			RenderPageLink(output, PageIndex, "", false, "pReload pButton");
			output.Write("</div>");

			if (_inputVisible)
			{
				//TODO:
				// RenderInput(output);
			}

			if (_summaryInfoVisible)
			{
				RenderSeparator(output);
				output.Write("<div class=\"pGroup\">");
				RenderSummaryInfo(output);
				output.Write("</div>");
			}
			output.Write("</div>"); 
		}

		protected override void RenderPageLink(TextWriter output, int pageIndex, bool isCurrentPage)
		{
			string displayText = (pageIndex + 1).ToString();
			if (!isCurrentPage)
			{
				displayText = string.Format("<div class=\"{0}\"><span>{1}</span></div>", "pButton pNumber", displayText);
				RenderPageLink(output, pageIndex, displayText, false);
			}
			else
			{
				output.Write("<div class=\"pButton current\">{0}</div>", displayText);
			}
		}

		protected virtual void RenderPageLink(TextWriter output, int pageIndex, string displayText, bool disabled, string css)
		{
			if (disabled)
			{
				css += " disabled";
			}
			string content = string.Format("<div class=\"{0}\" title=\"{1}\"></div>", css, displayText);
			RenderPageLink(output, pageIndex, content, disabled);
		}

		protected override void RenderPageLink(TextWriter output, int pageIndex, string displayText, bool disabled)
		{
			if (!disabled)
			{
				base.RenderPageLink(output, pageIndex, displayText, disabled);
			}
			else
			{
				output.Write("<span class=\"disabled\">{0}</span>", displayText);
			}
		}

		protected virtual void RenderSeparator(TextWriter output)
		{
			output.Write("<div class=\"btnseparator\"/>");
		}

		protected override void RenderSuspensionPoints(TextWriter output)
		{
			string displayText = string.Format("<div class=\"{0}\"><span>{1}</span></div>", "pButton", "...");
			RenderPageLink(output, -1, displayText, true);
		}
	}
}
